/**
 * 
 */
package com.xbang.attendance.entity.order;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.xbang.attendance.entity.product.Product;

/**
 * @author dev.zhang
 * 
 */
@Entity
public class PurchaseDetails implements Serializable {

	private static final long serialVersionUID = -1866501015466393038L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	/** PO */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity = PurchaseOrder.class, fetch = FetchType.LAZY)
	@JoinColumn(name = "purchase_order", referencedColumnName = "id")
	private PurchaseOrder purchaseOrder;

	/** 配件 */

	@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE }, targetEntity = Product.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "product_id", referencedColumnName = "id")
	private Product product;

	/** 配件数量 */
	private BigDecimal productNumber;

	/** 已发送数量 */
	private BigDecimal deliveryNumber = new BigDecimal(0);

	/** 剩余数量 */
	private BigDecimal balanceNumber = new BigDecimal(0);

	/** 运送服务价格--服务费 */
	private BigDecimal serviceAmount;

	public boolean isBalance() {
		balanceNumber = balanceNumber == null ? new BigDecimal(0) : balanceNumber;
		return balanceNumber.compareTo(new BigDecimal(0)) > 0;
	}

	public String getPercentage() {

		if (productNumber.compareTo(new BigDecimal(0)) == 0 || deliveryNumber.compareTo(new BigDecimal(0)) == 0) {
			return "<font color = '#FF0000'> <b> 0 </b> </font>";
		} else {
			BigDecimal percentage = deliveryNumber.divide(productNumber, 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
			if (percentage.intValue() < 99) {
				return "<font color = '#FF0000'> <b> ".concat(percentage.toString()).concat(" </b> </font>");
			} else {
				return "<font color = '#347C17'> <b> ".concat(percentage.toString()).concat(" </b> </font>");
			}
		}
	}

	/** 状态 */
	private String status;

	public PurchaseDetails() {
	}

	/**
	 * @return the purchaseOrder
	 */
	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	/**
	 * @param purchaseOrder
	 *            the purchaseOrder to set
	 */
	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product
	 *            the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * @return the productNumber
	 */
	public BigDecimal getProductNumber() {
		return productNumber;
	}

	/**
	 * @param productNumber
	 *            the productNumber to set
	 */
	public void setProductNumber(BigDecimal productNumber) {
		this.productNumber = productNumber;
	}

	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the deliveryNumber
	 */
	public BigDecimal getDeliveryNumber() {
		return deliveryNumber;
	}

	/**
	 * @param deliveryNumber
	 *            the deliveryNumber to set
	 */
	public void setDeliveryNumber(BigDecimal deliveryNumber) {
		this.deliveryNumber = deliveryNumber;
	}

	/**
	 * @return the balanceNumber
	 */
	public BigDecimal getBalanceNumber() {
		return balanceNumber;
	}

	/**
	 * @param balanceNumber
	 *            the balanceNumber to set
	 */
	public void setBalanceNumber(BigDecimal balanceNumber) {
		this.balanceNumber = balanceNumber;
	}

	/**
	 * @return the serviceAmount
	 */
	public BigDecimal getServiceAmount() {
		return serviceAmount;
	}

	/**
	 * @param serviceAmount
	 *            the serviceAmount to set
	 */
	public void setServiceAmount(BigDecimal serviceAmount) {
		this.serviceAmount = serviceAmount;
	}

}
